using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace TidhiGanitam.DataModel
{
    public class ChandraRoutineItem
    {
        int m_iIndex;
        int m_iBhagam;
        DateTime m_Date;
        RaviRoutineItem m_RaviRoutine;
        Rasi m_ChandraKendram;
        Rasi m_SpastaChandrudu;
        Rasi m_ChandraDinaGati;
        //ArrayList m_ChandraRoutines;
        MandaChandraItem m_MandaChandraItem;

        public ChandraRoutineItem(int Bhagam, Rasi SpastaChadrudu, Rasi ChandraKendram, RaviRoutineItem RaviRoutine, DateTime Date)
        {
            m_iBhagam = Bhagam;
            m_Date = Date;
            m_SpastaChandrudu = new Rasi(SpastaChadrudu);
            m_ChandraKendram = new Rasi(ChandraKendram);
            m_iIndex = Convert.ToInt32(m_ChandraKendram.Rasulu * 30 + m_ChandraKendram.Bhagalu);
            //m_MandaChandraItem = (MandaChandraItem)Constants.MandaChandraPadakalu[m_iIndex];
            m_MandaChandraItem = Constants.GetMandaChandraItem(m_iIndex);
            m_RaviRoutine = RaviRoutine;
        }
        public int Bhagam
        {
            get { return m_iBhagam; }
            set { m_iBhagam = value; }
        }
        public DateTime Date
        {
            get { return m_Date; }
        }
        public Rasi ChandraKendram
        {
            get { return m_ChandraKendram; }
        }
        public Rasi SpastaChandrudu
        {
            get { return m_SpastaChandrudu; }
        }
        public Rasi ChandraDinaGatiPhalam
        {
            get { return m_ChandraDinaGati; }
        }
        public void Calculate()
        {
            Rasi MandaPhalamDiff = new Rasi();
            Rasi GatiPhalamDiff = new Rasi();
            MandaChandraItem NextMandaChandraItem;
            //Step 1. Ravi Mandaphalam Dhanam 
            //Find out whether chandra kendram is between 0 - 6 signs or 6-12 signs.
            //becuse we need to perform a step later.
            bool bBetween0And6 = false;
            if (m_ChandraKendram < new Rasi(5, 29, 59, 59)) { bBetween0And6 = true; }
            if (m_RaviRoutine.Sign < 0)
            {
                m_SpastaChandrudu -= m_RaviRoutine.ChandraPhalam;
                m_ChandraKendram += m_RaviRoutine.ChandraPhalam;
            }
            else
            {
                m_SpastaChandrudu += m_RaviRoutine.ChandraPhalam;
                m_ChandraKendram -= m_RaviRoutine.ChandraPhalam;
            }
            //This becomes the actual chandra kendramu
            m_iIndex = Convert.ToInt32(m_ChandraKendram.Rasulu * 30 + m_ChandraKendram.Bhagalu);
            m_MandaChandraItem = Constants.GetMandaChandraItem(m_iIndex);
            //Step 1. Nakshatra phalam samskaram
            // if Chandra Kendram < 6 Rasulu Add Nakshatra Phalam to Spasta Chandrudu, 
            //                              Subtract Nakshatra Phalam to Chandra Kendram
            // else Subtract Nakshatra Phalam to Spasta Chandrudu,
            //                              Add Nakshatra Phalam to Chandra Kendram

            //Step 2. Calculate Chandra Manda Phalam Gati Phalam
            Rasi ChandraKendramAdvancement = new Rasi(0, 0, m_ChandraKendram.Liptalu, m_ChandraKendram.Viliptalu);
            if (m_iIndex < 360)
            {
                //NextMandaChandraItem = Constants.MandaChandraPadakalu[m_iIndex + 1];
                NextMandaChandraItem = Constants.GetMandaChandraItem(m_iIndex + 1);
                MandaPhalamDiff = Rasi.Difference(NextMandaChandraItem.MandaPadakam , m_MandaChandraItem.MandaPadakam);
                GatiPhalamDiff = Rasi.Difference(NextMandaChandraItem.Gati , m_MandaChandraItem.Gati);
                //if (m_MandaChandraItem.Gati < new Rasi(0d))
                //{ AddGati = false; }
                if (ChandraKendramAdvancement.Liptalu > 0 || ChandraKendramAdvancement.Viliptalu > 0)
                {
                    MandaPhalamDiff = ((Rasi)(MandaPhalamDiff * ChandraKendramAdvancement)).Round();
                    GatiPhalamDiff = ((Rasi)(GatiPhalamDiff * ChandraKendramAdvancement)).Round();
                }
                m_MandaChandraItem.MandaPadakam += MandaPhalamDiff;
                //m_ChandraDinaGati = m_MandaChandraItem.Gati + GatiPhalamDiff;
                m_MandaChandraItem.Gati += GatiPhalamDiff;
            }

            if (bBetween0And6)
            {
                m_SpastaChandrudu -= m_MandaChandraItem.MandaPadakam;
            }
            else
            {
                m_SpastaChandrudu += m_MandaChandraItem.MandaPadakam;
            }
            //Subtract gati phalam from constant chandra dina gati if gati phalam is -ve
            //Add gati phalama to constant chandra dina gati if gati phalama is +ve
            //We need not bother about adding and subtracting as the original values are
            //entered -ve or +ve. when you add them they are automatically taken care of.
            m_ChandraDinaGati = Constants.ChandraDinaGati + m_MandaChandraItem.Gati;

            m_ChandraKendram.Refresh();
            m_SpastaChandrudu.Refresh();
        }
    }
}
